package managesystem.poverty;

import managesystem.JDBCUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Poverty
 * @description: TODO
 * @date 2021/12/2 16:10
 */
public class PovertyJoinItem {
    JFrame frame = new JFrame();

    JPanel p = new JPanel();
    JPanel p1 = new JPanel(new GridLayout(11, 1, 20, 20));
    JPanel p2 = new JPanel(new GridLayout(11, 1, 20, 20));
    JPanel p3 = new JPanel();

    JLabel title = new JLabel("登记信息");

    JLabel help = new JLabel("帮扶人员编号：");
    JLabel item = new JLabel("项目编号：");
    JLabel name = new JLabel("姓名：");
    JLabel age = new JLabel("年龄：");
    JLabel sex = new JLabel("性别：");
    JLabel nation = new JLabel("民族：");
    JLabel birthplace = new JLabel("籍贯：");
    JLabel reason = new JLabel("贫困原因：");
    JLabel health = new JLabel("身体状况：");
    JLabel educated = new JLabel("受教育程度：");
    JLabel date = new JLabel("登记时间：");

    JTextField helpText = new JTextField(16);
    JTextField itemText = new JTextField(16);
    JTextField nameText = new JTextField(16);
    JTextField ageText = new JTextField(16);
    JTextField sexText = new JTextField(16);
    JTextField nationText = new JTextField(16);
    JTextField birthplaceText = new JTextField(16);
    JTextField reasonText = new JTextField(16);
    JTextField healthText = new JTextField(16);
    JTextField educatedText = new JTextField(16);
    JTextField dateText = new JTextField(16);

    JButton addItemButton = new JButton("加入");

//    public static void main(String[] args) {
//        new PovertyJoinItem().init();
//    }

    public void init() {
        opration();
        title.setFont(new Font("楷体", Font.BOLD, 40));

        help.setFont(new Font("微软雅黑", Font.BOLD, 16));
        item.setFont(new Font("微软雅黑", Font.BOLD, 16));
        name.setFont(new Font("微软雅黑", Font.BOLD, 16));
        age.setFont(new Font("微软雅黑", Font.BOLD, 16));
        sex.setFont(new Font("微软雅黑", Font.BOLD, 16));
        nation.setFont(new Font("微软雅黑", Font.BOLD, 16));
        birthplace.setFont(new Font("微软雅黑", Font.BOLD, 16));
        reason.setFont(new Font("微软雅黑", Font.BOLD, 16));
        health.setFont(new Font("微软雅黑", Font.BOLD, 16));
        educated.setFont(new Font("微软雅黑", Font.BOLD, 16));
        date.setFont(new Font("微软雅黑", Font.BOLD, 16));

        helpText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        itemText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        nameText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        ageText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        sexText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        nationText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        birthplaceText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        reasonText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        healthText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        educatedText.setFont(new Font("微软雅黑", Font.BOLD, 14));
        dateText.setFont(new Font("微软雅黑", Font.BOLD, 14));

        addItemButton.setFont(new Font("微软雅黑", Font.BOLD, 20));

        dateText.setText("格式为：YYYY-MM-DD");

        p.add(title);

        p1.add(help);
        p1.add(item);
        p1.add(name);
        p1.add(age);
        p1.add(sex);
        p1.add(nation);
        p1.add(birthplace);
        p1.add(reason);
        p1.add(health);
        p1.add(educated);
        p1.add(date);

        p2.add(helpText);
        p2.add(itemText);
        p2.add(nameText);
        p2.add(ageText);
        p2.add(sexText);
        p2.add(nationText);
        p2.add(birthplaceText);
        p2.add(reasonText);
        p2.add(healthText);
        p2.add(educatedText);
        p2.add(dateText);

        p3.add(addItemButton);

        frame.add(p, BorderLayout.NORTH);
        frame.add(p1, BorderLayout.WEST);
        frame.add(p2, BorderLayout.CENTER);
        frame.add(p3, BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setSize(500, 700);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void opration() {
        addItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!helpText.getText().equals("") && !itemText.getText().equals("") &&
                        !nameText.getText().equals("") && !ageText.getText().equals("") &&
                        !sexText.getText().equals("") && !nationText.getText().equals("") &&
                        !birthplaceText.getText().equals("") && !reasonText.getText().equals("") &&
                        !healthText.getText().equals("") && !educatedText.getText().equals("") &&
                        !dateText.getText().equals("") && !dateText.getText().equals("格式为：YYYY-MM-DD")) {

                    String insert = "insert into user_poor(hid, iid, pname, page, psex, pnation, birthplace, reason, health, educated, date) " +
                            "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    JDBCUtils.addPoverty(insert, helpText.getText(), itemText.getText(), nameText.getText(),
                            ageText.getText(), sexText.getText(), nationText.getText(), birthplaceText.getText(),
                            reasonText.getText(), healthText.getText(), educatedText.getText(), dateText.getText());

//                    frame.setVisible(false);
                    frame.dispose();
                    new PovertyShowAllInfo().init();
                }

                if (helpText.getText().equals("")) {
                    helpText.setForeground(Color.RED);
                    helpText.setText("此框不能为空！");
                }

                if (itemText.getText().equals("")) {
                    itemText.setForeground(Color.RED);
                    itemText.setText("此框不能为空！");
                }

                if (nameText.getText().equals("")) {
                    nameText.setForeground(Color.RED);
                    nameText.setText("此框不能为空！");
                }

                if (ageText.getText().equals("")) {
                    ageText.setForeground(Color.RED);
                    ageText.setText("此框不能为空！");
                }

                if (sexText.getText().equals("")) {
                    sexText.setForeground(Color.RED);
                    sexText.setText("此框不能为空！");
                }

                if (nationText.getText().equals("")) {
                    nationText.setForeground(Color.RED);
                    nationText.setText("此框不能为空！");
                }

                if (birthplaceText.getText().equals("")) {
                    birthplaceText.setForeground(Color.RED);
                    birthplaceText.setText("此框不能为空！");
                }

                if (reasonText.getText().equals("")) {
                    reasonText.setForeground(Color.RED);
                    reasonText.setText("此框不能为空！");
                }

                if (healthText.getText().equals("")) {
                    healthText.setForeground(Color.RED);
                    healthText.setText("此框不能为空！");
                }

                if (educatedText.getText().equals("")) {
                    educatedText.setForeground(Color.RED);
                    educatedText.setText("此框不能为空！");
                }

                if (dateText.getText().equals("")) {
                    dateText.setForeground(Color.RED);
                    dateText.setText("此框不能为空！");
                }

            }
        });

        helpText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                helpText.setText("");
                helpText.setForeground(Color.BLACK);
            }
        });

        itemText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                itemText.setText("");
                itemText.setForeground(Color.BLACK);
            }
        });

        nameText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                nameText.setText("");
                nameText.setForeground(Color.BLACK);
            }
        });

        ageText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ageText.setText("");
                ageText.setForeground(Color.BLACK);
            }
        });

        sexText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sexText.setText("");
                sexText.setForeground(Color.BLACK);
            }
        });

        nationText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                nationText.setText("");
                nationText.setForeground(Color.BLACK);
            }
        });

        birthplaceText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                birthplaceText.setText("");
                birthplaceText.setForeground(Color.BLACK);
            }
        });

        reasonText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                reasonText.setText("");
                reasonText.setForeground(Color.BLACK);
            }
        });

        healthText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                healthText.setText("");
                healthText.setForeground(Color.BLACK);
            }
        });

        educatedText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                educatedText.setText("");
                educatedText.setForeground(Color.BLACK);
            }
        });

        dateText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dateText.setText("");
                dateText.setForeground(Color.BLACK);
            }
        });
    }
}
